﻿using System;

using UnityEngine;

namespace GameCore
{

    
    public class TimerEvent
    {
      
        public string timerName = string.Empty;
        /// <summary>
        /// 重复调用次数,-1代表一直调用
        /// </summary>
        public int loopCount = 0;
        /// <summary>
        /// 当前调用次数
        /// </summary>
        public int curRepeat = 0;

        /// <summary>
        /// 是否忽略时间缩放
        /// </summary>
        public bool isIgnoreTimeScale = false;
        /// <summary>
        /// 回调方法
        /// </summary>
        public Action callBack;     
        /// <summary>
        /// 时间间隔
        /// </summary>
        public float timerSpace;
        /// <summary>
        /// 当前时间
        /// </summary>
        public float curTimer = 0;
        /// <summary>
        /// 是否完成
        /// </summary>
        public bool isDone = false;
        /// <summary>
        /// 是否停止
        /// </summary>
        public bool isStop = false;

        /// <summary>
        /// 更新
        /// </summary>
        public virtual void Update()
        {
            if (isIgnoreTimeScale)            
                curTimer += Time.unscaledDeltaTime;            
            else            
                curTimer += Time.deltaTime;
            isDone = curTimer >= timerSpace;
        }
        /// <summary>
        /// 完成
        /// </summary>
        public void CompleteTimer()
        {
            OnTimerCallBack();

            if (loopCount > 0)           
                curRepeat++;
            

            if (curRepeat != loopCount)
            {
                isDone = false;
                curTimer = 0;
            }
        }

        public virtual void OnTimerCallBack()
        {
            try
            {
                if (callBack != null)
                    callBack();
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
        public virtual void ResetTimer()
        {
            curTimer = 0;
            curRepeat = 0;
        }
        public virtual void Init() { }
        public virtual void OnPop()
        {
            timerName = string.Empty;
            loopCount = 0;
            curRepeat = 0;
            isIgnoreTimeScale = false;
            callBack = null;         
            timerSpace = 0; ;
            curTimer = 0;
            isDone = false;
            isStop = false;
        }
        public virtual void OnPush()
        {
            isStop = true;
        }
    }
    public class TimerEvent<T>:TimerEvent
    {
        /// <summary>
        /// 参数
        /// </summary>
        public T args;
        /// <summary>
        /// 含参数回调
        /// </summary>
        public Action<T> callBackArgs;
        public override void OnTimerCallBack()
        {
            base.OnTimerCallBack();
            try
            {
                if (callBackArgs != null)
                    callBackArgs(args);
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }
        public override void OnPop()
        {
            base.OnPop();
            args = default(T);
        }
    }
}